package uit.nfcmenu.view;

import java.util.ArrayList;

import uit.nfcmenu.db.model.OrderModel;
import uit.nfcmenu.manager.AssetsManager;
import uit.nfcmenu.util.CommConstant;
import uit.nfcmenu.webservice.JSONParser;
import uit.nfcmenu.webservice.PreferenceHelper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import uit.nfcmenu.R;

public class CookedListAdapter extends BaseAdapter {
//	private String tag = ChefListCookedAdapter.class.getSimpleName();

	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<OrderModel> mModels;
	String mLanguage;

	// constructor
	public CookedListAdapter(Context context, ArrayList<OrderModel> models) {
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
		mModels = models;
		mLanguage = PreferenceHelper.loadLanguage(mContext);
	}

	// get total amount of list items
	@Override
	public int getCount() {
		return mModels.size();
	}

	// get the list item given its position
	@Override
	public Object getItem(int position) {
		return mModels.get(position);
	}

	// get the list item's ItemModelName given its position
	@Override
	public long getItemId(int position) {
		return mModels.get(position).getId();

	}

	// Project view of the list
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater
					.inflate(R.layout.inflater_cooked_list, null);
			holder = new ViewHolder();
			holder.tvItem = (TextView) convertView.findViewById(R.id.tvItem);
			holder.tvTable = (TextView) convertView.findViewById(R.id.tvTable);
			holder.tvQuantity = (TextView) convertView.findViewById(R.id.tvQuantity);
			holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
			holder.row = (LinearLayout) convertView.findViewById(R.id.row);
			AssetsManager.setCommonAppFont(mContext, holder.tvItem);
			AssetsManager.setCommonAppFont(mContext, holder.tvQuantity);
			AssetsManager.setCommonAppFont(mContext, holder.tvTable);
			AssetsManager.setCommonAppFont(mContext, holder.tvTime);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// if the holder's not setup, initiate its content widgets
		OrderModel orderModel = mModels.get(position);
		if (orderModel != null) {
			holder.row.setSelected(position % 2 != 0);
	
			// get the selected schedule assign contents
			String itemName = CommConstant.item.get(orderModel.getItemID());
			if (itemName == null ) {
				mModels.remove(position);
				notifyDataSetChanged();
			}
			itemName = JSONParser.getStringByLanguage(itemName, mLanguage);
			holder.tvItem.setText("" + itemName);
			holder.tvTable.setText("" + orderModel.getTableNo());
			holder.tvQuantity.setText("" + orderModel.getQuantity());
			// view time // ex "Wed, 12 Sep 2012 02:41:19 GMT "
			String time = orderModel.getOrderTime();
			holder.tvTime.setText((time.length() > 25)?time.substring(0, 25):time);
		} 
		
		return convertView;
	}

	// view holder model
	class ViewHolder {
		TextView tvItem;
		TextView tvTable;
		TextView tvQuantity;
		TextView tvTime;
		LinearLayout row;
	}
	
	// Getter, Setter
	public ArrayList<OrderModel> getModels() {
		return mModels;
	}

	public void setModels(ArrayList<OrderModel> models) {
		mModels = models;
		notifyDataSetChanged();
		notifyDataSetInvalidated();
	}
}
